<template>
  <el-card class="box-card">
    <el-form :inline="true" :model="form">
      <el-form-item label="邮箱" prop="mail">
        <el-input v-model="form.mail" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item style="float: right">
        <el-button type="success" v-if="$formatFR(account.add)" @click="handleAdd">新增</el-button>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格部分 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column label="邮箱" align="center" prop="mail" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="SMTP 服务器域名" align="center" prop="host" />
      <el-table-column label="SMTP 服务器端口" align="center" prop="port" />
      <el-table-column label="是否开启 SSL" align="center" prop="sslEnable">
        <template v-slot="scope">
          <el-tag v-if="scope.row.sslEnable == 1">是</el-tag>
          <el-tag type="info" v-if="scope.row.sslEnable == 0">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template v-slot="scope">
          <el-button
            type="warning"
            size="mini"
            v-if="$formatFR(account.edit)"
            plain
            @click="handleUpdate(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            v-if="$formatFR(account.del)"
            plain
            @click="handleDel(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :size.sync="form.size"
      :current.sync="form.current"
      :total="total"
      @pagination="getList"
    />
    <!-- 弹窗角色 -->
    <AddEditDialog ref="AddEditDialog" @updatePage="reset" />
  </el-card>
</template>

<script>
import { page, del } from "@/api/message/mail/account"
import AddEditDialog from "./components/AddEditDialog"
import FUNCROLELIST from "@/utils/func_role/message"
export default {
  components: {
    AddEditDialog
  },
  data() {
    return {
      account: FUNCROLELIST.mail.account,
      form: {
        size: 10,
        current: 1,
        mail: null,
        username: null
      },
      total: 0,
      tableData: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    reset() {
      this.form = {
        createTime: [],
        size: 10,
        current: 1
      }
      this.getList()
    },
    handleAdd() {
      this.$refs.AddEditDialog.init(null, "新增")
    },
    handleDel(id) {
      this.$confirm("请确认是否删除当前内容?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        del(id).then(() => {
          this.getList()
          this.$message({
            type: "success",
            message: "删除成功!"
          })
        })
      })
    },
    handleUpdate(scope) {
      this.$refs.AddEditDialog.init(scope, "编辑")
    },
    search() {
      this.form.current = 1
      this.getList()
    },

    async getList() {
      const { data } = await page(this.form)
      this.tableData = data.records
      this.total = data.total
    }
  }
}
</script>

<style lang="scss" scoped>
.dropdown-item-p {
  display: inline;
}
.dropdown {
  margin-left: 10px;
}
</style>
